package mortal.game.control
{
    import Message.Public.*;
    import flash.geom.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.view.award.*;
    import mortal.game.view.shortcuts.item.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuideItemController extends Controller
    {
        private var _lastItem:ItemData;

        public function GuideItemController()
        {
            
        }

        protected function onGuideItemRes(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 != null)
            {
                if (_loc_2.itemInfo.item.category == ECategory._ECategoryDrug)
                {
                    if (_loc_2.type == EDrug._EDrugPetEnergy || _loc_2.type == EDrug._EDrugPetLifeSpan)
                    {
                    }
                    else if (ItemsUtil.isChangeShape(_loc_2))
                    {
                        if (cache.guide.autoType == GuideAutoType.FullAuto)
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, _loc_2));
                        }
                        else
                        {
                            view.show();
                            (view as GuideItemModule).updateData(_loc_2);
                        }
                    }
                    else
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, _loc_2));
                    }
                }
                else if (_loc_2.category == ECategory._ECategoryPet)
                {
                    cache.guide.petGuide = true;
                }
                else if (_loc_2.category == ECategory._ECategoryMount)
                {
                    cache.guide.guideMount = _loc_2;
                    if (_loc_2.posType == 0)
                    {
                        _loc_3 = cache.pack.backPackCache.getIndexByUid(_loc_2.uid);
                    }
                    _loc_3 = cache.pack.getPackChacheByPosType(_loc_2.posType).getIndexByUid(_loc_2.uid);
                    GameProxy.roleProxy.dress_async(_loc_2, EDressPos._EDressPosMount, _loc_2.posType, _loc_3);
                    Dispatcher.addEventListener(EventName.BackPackReqData, this.onPackReqDataRes);
                }
                else if (_loc_2.itemInfo.item.category == ECategory._ECategoryProp && _loc_2.itemInfo.item.type == EProp._EPropSkillBook)
                {
                    if (!cache.guide.guideSkillLearn)
                    {
                        cache.guide.skillLearnAlert = true;
                        GameProxy.skillProxy.learnByBook(_loc_2);
                    }
                }
                else if (cache.guide.autoType == GuideAutoType.FullAuto)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, _loc_2));
                }
                else
                {
                    view.show();
                    (view as GuideItemModule).updateData(_loc_2);
                    if (_loc_2.category == ECategory._ECategoryProp && _loc_2.type == EProp._EPropSkillBook)
                    {
                        cache.guide.skillLearnAlert = true;
                    }
                }
            }
            if (this._lastItem != null)
            {
                this.onItemUseReq(null);
            }
            this._lastItem = (view as GuideItemModule).item;
            
        }

        private function onPackReqDataRes(event:DataEvent) : void
        {
            if (cache.guide.guideMount != null && cache.guide.guideMount.uid == String(event.data))
            {
                Dispatcher.removeEventListener(EventName.BackPackReqData, this.onPackReqDataRes);
                Dispatcher.dispatchEvent(new DataEvent(EventName.CallMounts, cache.guide.guideMount));
                cache.guide.guideMount = null;
            }
            
        }

        private function onUseLevelAwardThing(event:DataEvent) : void
        {
            this.useItem(event.data as ItemData);
            
        }

        private function onItemUseReq(event:DataEvent) : void
        {
            var _loc_2:ItemData = null;
            if (event == null)
            {
                _loc_2 = this._lastItem;
            }
            else
            {
                _loc_2 = event.data as ItemData;
            }
            if (_loc_2 == null)
            {
                
            }
            this.useItem(_loc_2);
            this._lastItem = null;
            
        }

        private function useItem(param1:ItemData) : void
        {
            var _loc_2:String = null;
            var _loc_3:int = 0;
            if (param1.category == ECategory._ECategoryMount)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.CallMounts, param1));
            }
            else
            {
                _loc_2 = param1.uid;
                _loc_3 = cache.pack.backPackCache.getIndexByUid(_loc_2);
                if (_loc_3 != -1)
                {
                    if (param1.itemInfo.item.category == ECategory._ECategoryProp && param1.itemInfo.item.type == EProp._EPropSkillBook)
                    {
                        GameProxy.skillProxy.learnByBook(param1);
                    }
                    else if (param1.itemInfo.item.category == ECategory._ECategoryDrug && !ItemsUtil.isChangeShape(param1))
                    {
                    }
                    else if (param1.itemInfo.item.category == ECategory._ECategoryProp && param1.itemInfo.item.type == EProp._EPropVIPCard)
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ShowVipCard, {bmp:(view as GuideItemModule).itemBorder.bitmapdata, start:(view as GuideItemModule).localToGlobal(new Point((view as GuideItemModule).itemBorder.x, (view as GuideItemModule).itemBorder.y))}));
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, param1));
                    }
                    else
                    {
                        if (param1.itemInfo.item.category == ECategory._ECategoryEquip)
                        {
                            FlyToNavbarTool.flyToPalyer((view as GuideItemModule).itemBorder.bitmapdata, (view as GuideItemModule).localToGlobal(new Point((view as GuideItemModule).itemBorder.x, (view as GuideItemModule).itemBorder.y)));
                        }
                        else
                        {
                            FlyToNavbarTool.flyToBackPack((view as GuideItemModule).itemBorder.bitmapdata, (view as GuideItemModule).localToGlobal(new Point((view as GuideItemModule).itemBorder.x, (view as GuideItemModule).itemBorder.y)));
                        }
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, param1));
                    }
                }
            }
            
        }

        private function onSkillLearnGuideRes(param1:SkillInfo) : void
        {
            if (param1.tSkill.useType == ESkillUseType._ESkillUseTypeInitiative)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AddToShortcutsByReq, param1.tSkill));
            }
            
        }

        private function onBackPackUse(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            if (this._lastItem && this._lastItem.uid == _loc_2)
            {
                this._lastItem = null;
                view.hide();
            }
            
        }

        private function onMountsCallRes(param1:ItemData) : void
        {
            if (this._lastItem && this._lastItem.uid == param1.uid)
            {
                this._lastItem = null;
                view.hide();
            }
            
        }

        private function onSkillLearnRes(param1:SkillInfo) : void
        {
            if (!param1)
            {
                
            }
            if (this._lastItem && this._lastItem.itemInfo.item.category == ECategory._ECategoryProp && this._lastItem.itemInfo.item.type == EProp._EPropSkillBook)
            {
                if (this._lastItem.itemInfo.item.effect == param1.tSkill.skillId)
                {
                    this._lastItem = null;
                    view.hide();
                }
            }
            
        }

        private function onItemModuleClose(event:WindowEvent) : void
        {
            if (cache.guide.guideItemAlert)
            {
                cache.guide.guideItemAlert = false;
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ItemUsed));
            }
            if (this._lastItem && this._lastItem.itemInfo.item.category == ECategory._ECategoryProp && this._lastItem.itemInfo.item.type == EProp._EPropVIPCard)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ShowVipCard, {bmp:(view as GuideItemModule).itemBorder.bitmapdata, start:(view as GuideItemModule).localToGlobal(new Point((view as GuideItemModule).itemBorder.x, (view as GuideItemModule).itemBorder.y))}));
                Dispatcher.dispatchEvent(new DataEvent(EventName.ItemUseReq, this._lastItem));
                this._lastItem = null;
            }
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new GuideItemModule();
            _loc_1.addEventListener(EventName.Guide_ItemUse, this.onItemUseReq);
            _loc_1.addEventListener(WindowEvent.CLOSE, this.onItemModuleClose);
            _loc_1.layer = LayerManager.popupLayer;
            return _loc_1;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.GetTaskItemAndGuide, this.onGuideItemRes);
            Dispatcher.addEventListener(EventName.BackPackReqData, this.onBackPackUse);
            Dispatcher.addEventListener(EventName.UseLevelAwardThing, this.onUseLevelAwardThing);
            NetDispatcher.addCmdListener(ServerCommand.MountsCallRes, this.onMountsCallRes);
            NetDispatcher.addCmdListener(ServerCommand.SkillLearnGuideRes, this.onSkillLearnGuideRes);
            NetDispatcher.addCmdListener(ServerCommand.SkillUpdate, this.onSkillLearnRes);
            
        }

    }
}
